package com.ynny.jdbc01;

import com.mysql.cj.jdbc.Driver;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class JDBCDemo06 {

    public static void main(String[] args) throws SQLException {
        Driver driver = new Driver();
        DriverManager.registerDriver(driver);
        String url = "jdbc:mysql://localhost:3306/db01";
        String userName = "root";
        String password ="root";
        Connection connection = DriverManager.getConnection(url, userName, password);

        //4.得到PreparedStatement
        // 4.1携带参数
        //    ?  占位符

        String sql = "delete from book where id = ?";

        PreparedStatement preparedStatement =
                connection.prepareStatement(sql);
        //  4.2给占位符赋值
        //     parameterIndex 参数的下标 ----> 从1开始
             preparedStatement.setString(1,"4");
        //5.执行sql语句 ----->查询 用的是 executeQuery
        //             ----->删除 添加 修改
        int i = preparedStatement.executeUpdate();

        System.out.println(i);

        //7.关闭连接
        preparedStatement.close();
        connection.close();


    }
}

/**
 * 常见的错误分析：
 *  1.Unknown database 'db089898'  ----->找不到这个数据
 *  2.Access denied for user 'root'@'localhost' (using password: YES) ---->检查用户名 密码
 *  3.No suitable driver found for jdbc/://mysql://localhost:3306/db01
 *  4.com.mysql.cj.jdbc.Driver ----->没有导入jar
 *
 *
 */
